package com.edgardeng.education.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edgardeng.education.entity.JsonResponse;
import com.edgardeng.education.entity.Achievement;
import com.edgardeng.education.service.IAchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生成绩 前端控制器
 * </p>
 *
 * @author edgardeng
 * @since 2020-04-28
 */
@RestController
@RequestMapping("/api/achievement")
public class AchievementController extends BaseController {

    @Autowired
    IAchievementService baseService;

    @GetMapping()
    public IPage<Achievement> AchievementPage(
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            Achievement entity) {
        Page<Achievement> page = new Page<>(current, size); // current page and page size
        LambdaQueryWrapper<Achievement> queryWrapper = new LambdaQueryWrapper<>();
        if(entity != null){
            // TODO query condition
        }
        IPage<Achievement> AchievementPage = baseService.page(page, queryWrapper);
        return AchievementPage;
    }

    @PostMapping
    public JsonResponse addAchievement(@RequestBody Achievement entity) {
        boolean r = baseService.save(entity);
        return JsonResponse.result(r);
    }

    @GetMapping("/{id}")
    public Achievement getAchievement(@PathVariable Long id) {
        Achievement r = this.baseService.getById(id);
        return r;
    }

    @DeleteMapping("/{id}")
    public JsonResponse deleteAchievement(@PathVariable Long id) {
        boolean r = this.baseService.removeById(id);
        return JsonResponse.result(r);
    }

    @PutMapping()
    public JsonResponse updateAchievement(@RequestBody Achievement entity) {
        boolean r = this.baseService.updateById(entity);
        return JsonResponse.result(r);
    }
}
